package domain.controller;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Date;
import java.util.List;
import java.util.StringTokenizer;
import java.util.regex.Pattern;

import javax.jdo.PersistenceManager;
import javax.jdo.Query;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.sf.jsr107cache.Cache;
import net.sf.jsr107cache.CacheException;
import net.sf.jsr107cache.CacheManager;

import domain.data.QnAPersistenceManager;
import domain.model.Whois;
import domain.service.Utils;

@SuppressWarnings("serial")
public class SearchWhoisServlet extends HttpServlet {
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        resp.setContentType("text/html");
        resp.getWriter().println("Hello, world");
        Date date_cache = new Date(new Date().getTime()+60*60*1000);
		
//        String referer = req.getHeader("referer");   
//        System.out.println(referer);
//        resp.getWriter().println(referer);
//        if(referer == null)
//        {
//        	referer = "";
//        }
		resp.setHeader( "Cache-Control", "public, max-age=30");
		resp.setHeader( "Expires", date_cache.toGMTString() );
//        PersistenceManager psm = QnAPersistenceManager.get().getPersistenceManager();
//
//        String domain = Utils.fomatDomain(req.getParameter("name"));
//        if(domain != null && domain.length() > 0 && Utils.checkDomain(domain) && !Pattern.matches("^http://[^&]+&url=[\\w\\W]+", referer))
//        {
//	        domain = domain.toLowerCase().replaceAll("(http|https)+://(www.)*", "").replaceAll("^[\\w\\W]+[\\/]{2}", "").replaceAll("[\\/]+[\\w\\W]+$", "").replaceAll("www.", "");
//	        
//	        Query query = psm.newQuery(Whois.class);
//	        query.setFilter("name=='"+domain+"'");
//	        @SuppressWarnings("unchecked")
//			List<Whois> listWhois = (List<Whois>) query.execute();
//	        
//	        if(listWhois.size() > 0)
//	        {
//		        req.setAttribute("listWhois", listWhois.get(0));
//		        
//		        Cache cache=null;
//	
//		        try {
//		            cache = CacheManager.getInstance().getCacheFactory().createCache(Collections.emptyMap());
//		        } catch (CacheException e) {
//		           e.printStackTrace();
//		           
//		        }
//		        if(cache.containsKey("lastPingWhois"))
//		        {
//		        	@SuppressWarnings("unchecked")
//					ArrayList<String> lastPing = (ArrayList<String>)cache.get("lastPingWhois");
//		        	if(lastPing.size()>19)
//		        	{
//		        		lastPing.remove(0);
//		        		
//		        		
//		        	}
//		        	if(!lastPing.contains(listWhois.get(0).getName()))
//		        	lastPing.add(listWhois.get(0).getName());
//		        	cache.put("lastPingWhois", lastPing);
//		        	
//		        }
//		        else
//		        {
//		        	
//		        	ArrayList<String> lastPing = new ArrayList<String>();
//		        	if(!lastPing.contains(listWhois.get(0).getName()))
//		        	lastPing.add(listWhois.get(0).getName());
//		        	cache.put("lastPingWhois", lastPing);
//		        }
//		        
//		        req.setAttribute("title", "Whois lookup : Domain "+domain);
//				req.setAttribute("description", "Whois lookup : Domain "+domain+".Use Our Free Whois Lookup Database To Search For & Reserve Your Domain Today at  "+Utils.ValidDomain());
//				req.setAttribute("keyword", domain+",whois,whois lookup,Domain name information,  Server information, Web site analysis, website traffic details, Website backlinks website IP, website ranking info");
//		        
//		        try {
//					req.getRequestDispatcher("/whois.jsp").forward(req, resp);
//				} catch (ServletException e) {
//					// TODO Auto-generated catch block
//					e.printStackTrace();
//				}
//	        } else {
//	        	new domain.service.Whois(domain);
//	        	resp.sendRedirect("/whois/"+domain);
//	        }
//        }else {
//        	resp.sendError(resp.SC_NOT_FOUND,"PAGE NOT FOUND !");
//        }
    }
}
